package com.group1.ws;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hibernate.Session;

import com.group1.model.AnswerItem;
import com.group1.model.Question;
import com.group1.util.SessionFactoryUtil;

@Path("/answer")
public class AnswerWS {

	@GET
	@Path("/getAnswerByQuestID/{questionID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getAnswerByQuestID(@PathParam("questionID") String questionID){
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<AnswerItem> list = AnswerItem.getAllAnswByQuestion(Integer.valueOf(questionID), session);
		String result;
		result = "{ANSWER:[";
		boolean first = true;
		for (AnswerItem a : list) {
			Integer answerID = a.getAnsiId();
			String answerDesc = a.getAnsiDesc();
			String answerValue = a.getAnsiValue();
			boolean solution = a.getAnsiIsSolution();
			result += (first?"":",");
			result += "{";
			first = false;
			result += "\"answerID\"" + ":\"" + answerID + "\",";
			result += "\"questionID\"" + ":\"" + questionID + "\",";
			result += "\"answerDesc\"" + ":\"" + answerDesc + "\",";
			result += "\"answerValue\"" + ":\"" + answerValue + "\",";
			result += "\"solution\"" + ":\"" + solution + "\",";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getAnswerByID/{answerID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getAnswerByID(@PathParam("answerID") String answerID){
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		AnswerItem answer = AnswerItem.getAnswById(Integer.valueOf(answerID), session);
		String result;
		result = "{ANSWER:{";
		result += "\"answerID\"" + ":\"" + answerID + "\",";
		result += "\"questionID\"" + ":\"" + answer.getQuestion().getQuestionId() + "\",";
		result += "\"answerDesc\"" + ":\"" + answer.getAnsiDesc() + "\",";
		result += "\"answerValue\"" + ":\"" + answer.getAnsiValue() + "\",";
		result += "\"solution\"" + ":\"" + answer.getAnsiIsSolution() + "\",";
		result += "}}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/create/{questionID}/{answerDesc}/{answerValue}/{solution}")
	@Produces(MediaType.TEXT_PLAIN)
	public String createAnswer( @PathParam("questionID") String questionID, @PathParam("answerDesc") String answerDesc,
			@PathParam("answerValue") String answerValue, @PathParam("solution") String solution)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	AnswerItem ans = new AnswerItem();
			 	ans.setQuestion(Question.getQuestionByID(Integer.valueOf(questionID), session));
			 	ans.setAnsiDesc(answerDesc);
			 	ans.setAnsiValue(answerValue);
			 	ans.setAnsiIsSolution(Boolean.getBoolean(solution));
				session.save(ans);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	@GET
	@Path("/update/{answerID}/{questionID}/{answerDesc}/{answerValue}/{solution}")
	@Produces(MediaType.TEXT_PLAIN)
	public String updateAnswer(@PathParam("answerID") String answerID, @PathParam("questionID") String questionID, 
			@PathParam("answerDesc") String answerDesc,@PathParam("answerValue") String answerValue, @PathParam("solution") String solution)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	AnswerItem ans = AnswerItem.getAnswById(Integer.valueOf(answerID), session);
			 	ans.setQuestion(Question.getQuestionByID(Integer.valueOf(questionID), session));
			 	ans.setAnsiDesc(answerDesc);
			 	ans.setAnsiValue(answerValue);
			 	ans.setAnsiIsSolution(Boolean.valueOf(solution));
				session.saveOrUpdate(ans);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	
	@GET
	@Path("/delete/{answerID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String deleteAnswer(@PathParam("answerID") String answerID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 session.delete(AnswerItem.getAnswById(Integer.valueOf(answerID), session));
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
}
